package sanshui.system.trade.factory.data;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import sanshui.system.trade.config.SpringContextHolder;
import sanshui.system.trade.pojo.StockInfoVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供全局使用的数据缓存容器
 *
 * */

@Slf4j
@Data
@Component
public class TradeDataContainer {
    TradeMarketKey marketKey;
    TradeSystemType tradeSystemType;
    DataMarketType market;

    DataFactory dataFactory = SpringContextHolder.getBean(DataFactory.class);

    List<String> allCodeCache = new ArrayList<>();

    Map<String, StockInfoVo> allDataPerCycle = new HashMap<>();
    Map<String, List<StockInfoVo>> allData = new HashMap<>();

    private boolean isStop = false;
    private final BigDecimal minTradeNum = new BigDecimal("100");


    public static TradeDataContainer create() {
        return new TradeDataContainer();
    }

    public boolean isRunning() {
        return !this.isStop;
    }

    public void stop() {
        this.isStop = true;
        this.allData = null;
        this.allDataPerCycle = null;
        this.allCodeCache = null;
    }

    public Map<String, StockInfoVo> next(List<String> targetCode, int index, String startDate, String endDate) {
        Map<String, StockInfoVo> nextInfo = new HashMap<>();
        if (TradeSystemType.BACK_TRADE.compareTo(this.getTradeSystemType()) == 0 && !CollectionUtils.isEmpty(targetCode)){
            for (String code : targetCode) {
                List<StockInfoVo> hisStockInfo = allData.get(code);
                if (hisStockInfo.size() == 0){
                    List<StockInfoVo> hisStockInfoList = dataFactory.getHisStockInfo(code, startDate, endDate);
                    if (null == hisStockInfoList || hisStockInfoList.size() == 0){
                        log.warn("没有获取到信息, stockInfoVo: null");
                        return null;
                    } else {
                        allData.put(code, hisStockInfoList);
                    }
                }
                if (index >= hisStockInfo.size()){
                    log.info("历史数据使用完成");
                    return null;
                }
                nextInfo.put(code, hisStockInfo.get(index));
            }
            return nextInfo;
        }
        log.error("没有对应的TradeSystemType");
        return nextInfo;
    }


    public List<String> getAllCodesFilter(FilterModel filterModel) {
        List<String> targetCodes = new ArrayList<>();
        for (String code : allCodeCache) {
            if (filterModel.isBJ && code.startsWith("")){
                targetCodes.add(code);
            }
            if (filterModel.isCY && code.startsWith("300")){
                targetCodes.add(code);
            }

        }

        return targetCodes;
    }
}
